package features.advance.leetcode.interview.offer.easy;

import features.advance.leetcode.Solution;

/**
 * @author LIN
 * @date 2021-05-06
 */
public class Offer05 extends Solution {

    public String replaceSpace(String s){

        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            sb.append(aChar == 32 ? "%20" : aChar + "");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "We are happy.";
        Offer05 offer05 = new Offer05() {
            @Override
            public String replaceSpace(String s) {
                int length = s.length();
                char[] array = new char[length * 3];
                int size = 0;
                for (int i = 0; i < length; i++) {
                    char c = s.charAt(i);
                    if (c == ' ') {
                        array[size++] = '%';
                        array[size++] = '2';
                        array[size++] = '0';
                    } else {
                        array[size++] = c;
                    }
                }
                String newStr = new String(array, 0, size);
                return newStr;
            }
        };

        String s = offer05.replaceSpace(str);
        System.out.println(s);
    }
}
